﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PROJECT_BOMBER.BLL;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using PROJECT_BOMBER.BLL.Events;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
using System.Diagnostics;

namespace PROJECT_BOMBER.BLL.States
{
    public class WinState : GameState
    {
        internal AnimationSprite img;
        internal MenuButton button1;
        internal MouseEvent mouseEvent;
        Song winn;
        internal Character _character = null;
        internal int value1 = 0;
        internal int value2 = 0;
        internal int value3 = 0;

        internal Stopwatch chrono;
        internal double timebefore;
        internal int scorePiece = 0;
        internal int TotalPiece = 220;
        internal int scoreTemps = 0;

        SpriteFont spritetext;

        public WinState(Character character)
        {
            _character = character;
            Globals.Chronometre.Start = false;
        }

        public override void Initialize()
        {

            chrono = new Stopwatch();

            AnimationDefinition def = new AnimationDefinition("winstate", new Microsoft.Xna.Framework.Point(972, 685), new Microsoft.Xna.Framework.Point(2, 1), 2, true);

            img = new AnimationSprite(Globals.Game, def);

            img.Initialize();

            img.Position = new Point((Globals.GDM.PreferredBackBufferWidth - 972) / 2, (Globals.GDM.PreferredBackBufferHeight - 685) / 2);

            button1 = new MenuButton(new Vector2((Globals.GDM.PreferredBackBufferWidth - 300), Globals.GDM.PreferredBackBufferHeight), Globals.Content.Load<Texture2D>("bouton"), Globals.Content.Load<Texture2D>("bouton"), new Microsoft.Xna.Framework.Rectangle());
            Globals.Game.IsMouseVisible = true;
            mouseEvent = new MouseEvent();

            MediaPlayer.Stop();


            foreach (var frame in Globals.Level.Listframe)
	        {
		        foreach (var item in frame.listElement)
	            {
		            if(item.GetType() == typeof(Piece))
                    {
                        TotalPiece += ((Piece)item).Valeur;
                    }
	            }
	        }

            scorePiece = (int)((float)((float)_character.Score / (float)TotalPiece) * 50);
            scoreTemps = 6 * (int)((float)((float)Globals.Level.Listframe.Count / (float)Globals.Chronometre.Seconde) * 50);

            
        }

        public override void Update(float deltatime)
        {
            if (!chrono.IsRunning)
            {
                chrono.Restart();
            }

            if (timebefore*2 != chrono.ElapsedTicks && value1 < _character.Score)
            {
                value1++;
            }

            if (timebefore*2 != chrono.ElapsedTicks && value2 < Globals.Chronometre.Seconde && value1 == _character.Score)
            {
                value2++;
            }

            if (timebefore * 2 != chrono.ElapsedTicks && value3 < scoreTemps + scorePiece && value2 == Globals.Chronometre.Seconde)
            {
                value3++;
            }

            timebefore = (double)Math.Round((double)chrono.ElapsedTicks / 2);

            img.Update(img.Position.X, img.Position.Y);
            if (mouseEvent.UpdateMouse() == true)
            {
                Globals.Game.IsMouseVisible = false;
                Globals.GameStateStack.Set(new LoadState(new PlayState()));
            }

            KeyboardState ks = Keyboard.GetState();
            if (ks.IsKeyDown(Keys.S))
            {
                Globals.Game.IsMouseVisible = false;
                Globals.GameStateStack.Set(new LoadState(new PlayState()));
            }
        }

        public override void Draw()
        {
            Globals.SpriteBatch.Begin();

            // Globals.SpriteBatch.DrawString(Globals.ArialFont, WelcomeText, WelcomeTextPosition, Color.Red);
            button1.DrawButton(Globals.SpriteBatch);

            Globals.GDM.GraphicsDevice.Clear(Color.Black);
            Globals.SpriteBatch.End();

            Globals.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            img.Draw();
            Globals.SpriteBatch.End();

            Globals.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

            Globals.SpriteBatch.DrawString(spritetext, "Score piece : " + value1.ToString(), Vector2.Zero, Microsoft.Xna.Framework.Color.Red);
            Globals.SpriteBatch.DrawString(spritetext, "Score temps : " + value2.ToString(), new Vector2(400, 0), Microsoft.Xna.Framework.Color.Red);
            Globals.SpriteBatch.DrawString(spritetext, "Score total : " + value3.ToString(), new Vector2(800, 0), Microsoft.Xna.Framework.Color.Red);

            Globals.SpriteBatch.End();
        }

        public override void Load()
        {
            img.LoadContent(Globals.SpriteBatch);
            winn = Globals.Content.Load<Song>("Victory_Fanfare");
            MediaPlayer.Play(winn);
            spritetext = Globals.Content.Load<SpriteFont>("Arial");
        }

        public override void Unload()
        {

        }
    }
}
